package schedule;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import common.Service;
import entities.TTransaction;
import interfaces.IGlobalService;
import interfaces.ITransactionService;
import util.LogUtil;
import utils.DateUtil;

public class LogicSchedule implements Job {

	private IGlobalService globalService = (IGlobalService) Service.create(IGlobalService.class);
	private ITransactionService transactionService = (ITransactionService) Service.create(ITransactionService.class);

	public void execute(JobExecutionContext context) throws JobExecutionException {
		
		System.out.println(DateUtil.format(new Date(), DateUtil.DATETIME) + " | 物流超期处理任务启动");
		
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" select txn.txn_no 'txnNo' , txn.txn_amount 'txnAmount' ");
			sb.append(" from t_transaction txn join t_merchant mer on txn.mer_no=mer.mer_no  ");
			sb.append(" where txn.txn_logic='0' and txn.txn_state='1' and datediff(curdate(),txn.txn_date)>mer.mer_max ");
			QuerySet<Object> querySet = new QuerySet<Object>();
			querySet.setSqlInfo(sb.toString());
			querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
			ResultSet<LinkedHashMap<String, Object>> resultSet = globalService.search(querySet);
			List<LinkedHashMap<String, Object>> list = resultSet.getResultList();
			LogUtil.schedule("LOGIC", "今日物流共 "+list.size()+" 笔");
			for(LinkedHashMap<String, Object> map: list){
				TTransaction transaction=new TTransaction();
				transaction.setTxnNo(map.get("txnNo").toString());
				transaction.setTxnLogic("1");
				transactionService.persist(transaction);
				LogUtil.schedule("LOGIC", map.get("txnNo").toString()+"|"+map.get("txnAmount").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public static void main(String[] args) {
		IGlobalService globalService = (IGlobalService) Service.create(IGlobalService.class);
		ITransactionService transactionService = (ITransactionService) Service.create(ITransactionService.class);
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" select txn.txn_no 'txnNo' , txn.txn_amount 'txnAmount' ");
			sb.append(" from t_transaction txn join t_merchant mer on txn.mer_no=mer.mer_no  ");
			sb.append(" where txn.txn_logic='1' and txn.txn_settle='0' and txn.txn_state='1' and datediff(curdate(),txn.txn_date)>mer.mer_min ");
			QuerySet<Object> querySet = new QuerySet<Object>();
			querySet.setSqlInfo(sb.toString());
			querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
			ResultSet<LinkedHashMap<String, Object>> resultSet = globalService.search(querySet);
			List<LinkedHashMap<String, Object>> list = resultSet.getResultList();
			LogUtil.schedule("SETTLE", "今日结算交易共 "+list.size()+" 笔");
			for(LinkedHashMap<String, Object> map: list){
				TTransaction transaction=new TTransaction();
				transaction.setTxnNo(map.get("txnNo").toString());
				transaction.setTxnSettle("1");
				transactionService.persist(transaction);
				LogUtil.schedule("SETTLE", map.get("txnNo").toString()+"|"+map.get("txnAmount").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
